package demo1;

public class SingleLinkedList {
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }


    ListNode head;
    public ListNode removeElements(ListNode head, int val) {
        if(head == null){
            return head;
        }
      ListNode cur = head;
      while(cur.next != null){
          ListNode curN = cur.next;
          if(curN.val == val){
              cur.next = curN.next;
          }else {
              cur = cur.next;
          }
      }
      if(head.val == val){
          head = head.next;
      }
      return head;
    }
    public ListNode reverseList(ListNode head) {
        if(head == null || head.next == null){
            return head;
        }
      ListNode cur = head.next;
        head.next = null;
      while(cur.next != null){
         ListNode curN = cur.next;
         cur.next = head;
         head = cur;
         cur = curN;
      }
      cur.next = head;
       head = cur;
      return head;
    }
    public ListNode middleNode(ListNode head) {
      ListNode cur = head;
      while(cur != null && cur.next != null){
          head = head.next;
          cur = cur.next.next;
      }
      return head;
    }
    public int kthToLast(ListNode head, int k) {

       ListNode cur = head;
       for(int i = 1 ; i < k; i++){
               cur = cur.next;
           }

       while(cur.next != null){
           cur = cur.next;
           head = head.next;
       }
       return head.val;
    }
    public ListNode partition(ListNode pHead, int x) {
        ListNode node1 = new ListNode(1);
        ListNode head1 = node1;
        ListNode node2 = new ListNode(1);
        ListNode head2 = node2;
        while(pHead != null){
            if(pHead.val < x){
                head1.next = pHead;
                head1 = head1.next;
            }else {
                head2.next = pHead;
                head2 = head2.next;
            }
            pHead = pHead.next;
        }
        head2.next = null;
        head1.next = node2.next;
        return node1.next;
    }
    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = this.head;
        this.head = node;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        if(this.head == null){
            head = node;
        }
        ListNode cur = this.head;
        while(cur.next != null){
            cur = cur.next;
        }
        cur.next = node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if(index == 1){
            addFirst(data);
            return;
        }
        if(index == size() + 1){
            addLast(data);
            return;
        }
        ListNode cur = head;
        ListNode node = new ListNode(data);
        for(int i = 0 ; i < index - 2 ; i++){
            cur = cur.next;
        }
        node.next = cur;
        cur.next = node;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        if(this.head == null){
            return false;
        }
        ListNode cur = this.head;
        while(cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode cur = head;
        while(cur.next != null){
            ListNode pear = cur.next;
            if(pear.val == key){
                cur.next = pear.next;
            }
            cur = cur.next;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
     ListNode cur = this.head;
     if(head == null){
         return;
     }

     while(cur.next != null){
         ListNode curN = cur.next;
         if(curN.val == key){
             cur.next = curN.next;
         }else {
             cur = cur.next;
         }
     }
        if(head.val == key){
            head = head.next;
        }
    }
    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = head;
        while(cur != null){
            cur = cur.next;
            count++;
        }
        return count;
    }
    public void display(){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val +" ");
        }
        System.out.println();
    }
    public void clear(){
        head = null;
    }
}
